package com.longcai.controller.clyjuser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.customer.ClyjUserAgent;
import com.longcai.mapper.clyjuser.ClyjUserAgentMapper;
import com.longcai.mapper.clyjuser.ClyjUserMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjuser.ClyjUserAgentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户代理表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-05-10
 */
@RestController
@RequestMapping("/clyjUserAgent")
public class ClyjUserAgentController {

    @Autowired
    private ClyjUserAgentService clyjUserAgentService;
    @Autowired
    private ClyjUserAgentMapper clyjUserAgentMapper;
    @Autowired
    private ClyjUserMapper clyjUserMapper;

    /**
     * 获取代理商列表
     *
     * @param clyjUserAgent
     * @param pageHelper
     * @return
     */
    @ApiOperation(value = "代理商列表")
    @GetMapping(value = "/agentList")
    public PageTableResponse agentList(ClyjUserAgent clyjUserAgent, PageHelper pageHelper) {

        return this.clyjUserAgentService.agentList(clyjUserAgent, pageHelper);
    }

    /**
     * 根据id获取代理商信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取代理商信息")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('agent:query')")
    public ApiResult getById(@PathVariable String id) {

        return this.clyjUserAgentService.getById(id);
    }

    /**
     * 查询用户
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "查询用户")
    @GetMapping("/seach/{phone}")
    @PreAuthorize("hasAuthority('agent:query')")
    public ApiResult getByphone(@PathVariable String phone) {
        //根据手机号查询改用户存不存在
        QueryWrapper<ClyjUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        ClyjUser clyjUser = this.clyjUserMapper.selectOne(queryWrapper);
        if (clyjUser == null) {
            return ApiResult.success(null);
        }
        //查询改用户是否是代理（是代理返回null）
        QueryWrapper<ClyjUserAgent> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("phone", phone);
        ClyjUserAgent clyjUserAgent = this.clyjUserAgentMapper.selectOne(queryWrapper1);
        if (clyjUserAgent != null) {
            return ApiResult.success(null);
        }
        return ApiResult.success(clyjUser);
    }

    /**
     * 删除
     *
     * @param id
     */
    @PostMapping("{id}")
    @PreAuthorize("hasAuthority('agent:del')")
    public void delById(@PathVariable String id) {
        this.clyjUserAgentService.delById(id);
    }


    /**
     * 修改代理信息
     *
     * @param clyjUserAgent
     * @return
     */
    @PostMapping("/updatebyagent")
    @PreAuthorize("hasAuthority('agent:update')")
    public ApiResult updatebyagent(ClyjUserAgent clyjUserAgent) {
        if (clyjUserAgent.getId() == null || clyjUserAgent.getId().equals("")) {
            return this.clyjUserAgentService.saveagent(clyjUserAgent);
        }
        return this.clyjUserAgentService.updatebyagent(clyjUserAgent);
    }
}

